<?Lassoscript 
// Last modified 11/30/07 by ECL, Landmann InterActive

// FUNCTIONALITY
// This establishes the queries and results pages for the admin Testimonials area
// The controlling record is ID from the testimonials table, which is passed as the action_param "ID"

// CHANGE NOTES
// 11/30/07
// First implementation

// Start the session
Session_Start(-Name=$svSessionAdminName, -Expires=$svSessionTimeout);

If(!(Var_Defined('SQLQueryTestimonials'))); Session_AddVariable(-Name=$svSessionAdminName, 'SQLQueryTestimonials'); /If;

// Debugging
// Var('svDebug' = 'Y');

// Setting Variables for Nav Control
Var('vNewQuery' = Action_Param('NewQuery'));
Var('Records_Skipped' = Action_Param('skip'));
Var('This_Group' = Action_Param('group'));
Var('Records_Per_Page' = 10);
Var('SkipCount' = integer);
Var('Records_Found' = integer);
Var('Group_Size' = (Math_Mult(Var('Records_Per_Page'),5)));

If($svDebug == 'Y');
	'<p class="debug"><strong>testimonials_searchresults</strong></p>\n';
/If;

// Create the query if NewQuery = Y
If(Var('vNewQuery') == 'Y');

	Var('vSearchFor' = Action_Param('SearchFor'));
	Var('vActive' = Action_Param('Active'));

	Var('SQLSearchFor' = string);
	Var('SQLActive' = string);

	Var('SQLQueryTestimonials' = '');
	Var('SQLQuerySuffix' = '');
	Var('svSearch_Params' = '');
	Var('svSearch_ParamsURL' = '');

	// Include the code for building the search params phrase
	Include(($svAdminHelpersPath)+'searchparamstestimonials_build.inc');

/If;

// Define the suffix (same for any query)
Var('SQLQuerySuffix' = ' ORDER BY Testimonial_Head');

// Constructing the query
If(Var('SQLQueryTestimonials') == '');

	Var('SQLQueryTestimonials' = string);
	Var('SQLQueryLimit' = string);
	Var('SQLQueryTestimonials' = ('SELECT * FROM '+($svSiteDatabase)+'.'+($svTestimonialsTable)+' WHERE '));

	// SEARCH FOR
	If(Var('vSearchFor') != '');
		Var('SQLSearchFor' = (StripBackticks('Testimonial_Head LIKE "%'+($vSearchFor)+'%" OR
		Testimonial_Name LIKE "%'+($vSearchFor)+'%" OR 
		Testimonial_Comment LIKE "%'+($vSearchFor)+'%"')));

		$SQLQueryTestimonials += $SQLSearchFor;
		If(Var('vActive') != '');
			$SQLQueryTestimonials += ' AND ';
		/If;
	/If;

	// ACTIVE
	If(Var('vActive') != '');
		Var('SQLActive' = (StripBackticks('Active = "'+($vActive)+'"')));
		$SQLQueryTestimonials += $SQLActive;
	/If;


	// Add suffix
	$SQLQueryTestimonials += $SQLQuerySuffix;

	// SEARCHES EVERYTHING
	// If no search params passed, redefine the query to get everything
	If(($vSearchFor == '') && ($vActive == ''));
		Var('SQLQueryTestimonials' = (StripBackticks('SELECT * FROM '+($svSiteDatabase)+'.'+($svTestimonialsTable)+' ORDER BY Testimonial_Head ASC')));
	/If;

	If($svDebug == 'Y');
		('116: vSearchFor = '+(Var('vSearchFor'))+'<br>\n');
		('116: vActive = '+(Var('vActive'))+'<br>\n');
	/If;

/If;
	
// Performing Search
Inline($IV_Testimonials, -skiprecords=$Records_Skipped, -MaxRecords=$Records_Per_Page, -SQL=$SQLQueryTestimonials);

	If($svDebug == 'Y');
		('131: Error = '+(Error_CurrentError)+'<br>\n');
		('131: Found_Count = '+(Found_Count)+'<br>\n');
	/If;
	
	// If no records found, immediate redirect with error
	If(Found_Count == 0);
		Var('vError' = '2000');
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svSearchPage)),-UseError='Y',-Error=$vError,-UseArgs='Y');
	/If;

	// If only one record, redirect immediately to editrecord
	// This shortcut allows users to skip past the search results page if there is only one record found
	If(Found_Count == 1);
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&ID='+(Field('ID'))+'&New=Y'),-UseError='N',-UseArgs='N');
	/If;
?>
<!-- Search Results-->
<table width="525">
	<tr>
		<td colspan="5" valign="top">
			<div align="left">
				<h2>[LI_ShowIconByDataType]&nbsp;&nbsp;Testimonials Search Results</h2>
				<p><font color="red">[Var:'svSearch_Params', -EncodeNone]</font>&nbsp;&nbsp;
				<a href="http://[$svDomain][$svAdminControllersPath][$svSearchPage]/[$svSearch_ParamsURL]">Search Again</a></p>
			</div>
		</td>
	</tr>
	<tr bgcolor="#000000">
		<td width="20" valign="middle" class="tablehead_10_white"><strong>ID</strong></td>
		<td width="150" valign="middle" class="tablehead_10_white"><strong>Headline</strong></td>
		<td width="370" valign="middle" class="tablehead_10_white"><strong>Comment</strong></td>
		<td width="20" valign="middle" class="tablehead_10_white"><strong>Active</strong></td>
	</tr>
<?Lassoscript
Records;
	Var('vID' = Field('ID'));
	Var('vTestimonial_Head' = Field('Testimonial_Head'));
	Var('vTestimonial_Comment' = Field('Testimonial_Comment'));
	Var('vActive' = Field('Active'));
?>	<tr <?Lassoscript If((Loop_Count) %2 == 0); 'bgcolor="#F5F5F5"'; Else; 'bgcolor="#FFFFFF"'; /If; ?>>
		<td valign="top" width="20" class="tabletext_10_black">
			<strong><a href="[$svAdminControllersPath][$svEditRecordPage]?ID=[Var('vID')]&DataType=[Var('vDatatype')]&New=Y">[Var('vID')]</a></strong></td>
		<td valign="top" width="150" class="tabletext_10_black">
			<strong>[$vTestimonial_Head]</strong>
		</td>
		<td valign="top" width="270" class="tabletext_10_black">
			[$vTestimonial_Comment]
		</td>
		<td valign="top" width="20" class="tabletext_10_black">
			[$vActive]
		</td>
	</tr>
[/Records]
	<tr>
		<td colspan="5" valign="top" bgcolor="#cccccc">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="5" valign="middle" align="left" bgcolor="#f5f5f5">
			[Include(($svAdminHelpersPath)+'nav_control.inc')]
		</td>
	</tr>
</table>
[/Inline]
<br>
